package net.shopxx.service.impl;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import net.shopxx.dao.DeptDao;
import net.shopxx.entity.Admin;
import net.shopxx.entity.Dept;
import net.shopxx.service.DeptService;

import org.hibernate.Hibernate;
import org.springframework.stereotype.Service;
import org.springmodules.cache.annotations.CacheFlush;
import org.springmodules.cache.annotations.Cacheable;

/**
 * Service实现类 - 部门
 */

@Service
public class DeptServiceImpl extends BaseServiceImpl<Dept, String> implements DeptService {

	@Resource
	private DeptDao deptDao;
	
	@Resource
	public void setBaseDao(DeptDao deptDao) {
		super.setBaseDao(deptDao);
	}

	@Cacheable(modelId = "caching")
	public List<Dept> getRootDeptList() {
		List<Dept> rootDeptList = deptDao.getRootDeptList();
		if (rootDeptList != null) {
			for (Dept rootDept : rootDeptList) {
				Hibernate.initialize(rootDept);
			}
		}
		return rootDeptList;
	}
	
	@Cacheable(modelId = "caching")
	public List<Dept> getParentDeptList(Dept dept) {
		List<Dept> parentDeptList = deptDao.getParentDeptList(dept);
		if (parentDeptList != null) {
			for (Dept parentDept : parentDeptList) {
				Hibernate.initialize(parentDept);
			}
		}
		return parentDeptList;
	}
	
	public List<Dept> getParentDeptList(Admin admin) {
		Dept dept = admin.getDept();
		List<Dept> deptList = new ArrayList<Dept>();
		deptList.addAll(this.getParentDeptList(dept));
		deptList.add(dept);
		return deptList;
	}
	
	public List<Dept> getDeptPathList(Dept dept) {
		List<Dept> deptPathList = new ArrayList<Dept>();
		deptPathList.addAll(this.getParentDeptList(dept));
		deptPathList.add(dept);
		return deptPathList;
	}
	
	public List<Dept> getDeptPathList(Admin admin) {
		Dept dept = admin.getDept();
		List<Dept> deptList = new ArrayList<Dept>();
		deptList.addAll(this.getParentDeptList(dept));
		deptList.add(dept);
		return deptList;
	}
	
	@Cacheable(modelId = "caching")
	public List<Dept> getChildrenDeptList(Dept dept) {
		List<Dept> childrenDeptList = deptDao.getChildrenDeptList(dept);
		if (childrenDeptList != null) {
			for (Dept childrenDept : childrenDeptList) {
				Hibernate.initialize(childrenDept);
			}
		}
		return childrenDeptList;
	}
	
	public List<Dept> getChildrenDeptList(Admin admin) {
		Dept dept = admin.getDept();
		List<Dept> deptList = getChildrenDeptList(dept);
		if (deptList == null) {
			deptList = new ArrayList<Dept>();
		}
		deptList.add(dept);
		return deptList;
	}
	
	@Cacheable(modelId = "caching")
	public List<Dept> getDeptTreeList() {
		List<Dept> allDeptList = this.getAll();
		return recursivDeptTreeList(allDeptList, null, null);
	}
	
	// 递归父类排序分类树
	private List<Dept> recursivDeptTreeList(List<Dept> allDeptList, Dept p, List<Dept> temp) {
		if (temp == null) {
			temp = new ArrayList<Dept>();
		}
		for (Dept dept : allDeptList) {
			Dept parent = dept.getParent();
			if ((p == null && parent == null) || (dept != null && parent == p)) {
				temp.add(dept);
				if (dept.getChildren() != null && dept.getChildren().size() > 0) {
					recursivDeptTreeList(allDeptList, dept, temp);
				}
			}
		}
		return temp;
	}

	@Override
	@Cacheable(modelId = "caching")
	public List<Dept> getAll() {
		List<Dept> allDept = deptDao.getAll();
		if (allDept != null) {
			for (Dept dept : allDept) {
				Hibernate.initialize(dept);
			}
		}
		return allDept;
	}

	@Override
	@CacheFlush(modelId = "flushing")
	public void delete(Dept dept) {
		deptDao.delete(dept);
	}

	@Override
	@CacheFlush(modelId = "flushing")
	public void delete(String id) {
		deptDao.delete(id);
	}

	@Override
	@CacheFlush(modelId = "flushing")
	public void delete(String[] ids) {
		deptDao.delete(ids);
	}

	@Override
	@CacheFlush(modelId = "flushing")
	public String save(Dept dept) {
		return deptDao.save(dept);
	}

	@Override
	@CacheFlush(modelId = "flushing")
	public void update(Dept dept) {
		deptDao.update(dept);
	}
}